package com.joycessh.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.joycessh.bean.Orgization;
import com.joycessh.bean.Position;
import com.joycessh.dao.SettingDao;
import com.joycessh.util.DBConnectionManager;

public class SettingDaoImpl implements SettingDao {
	
	/**
	 * @author joycessh
	 * 接口SettingDao的实现类
	 */
	
	DBConnectionManager dcm = null;
	Connection conn = null;
	Statement  stmt = null;
	ResultSet rs = null;
	PreparedStatement prepstmt = null;
	String sqlStr ="";
	Boolean flag = false;
	int effectLines = 0;
	
	public boolean setOrg(Orgization orgization) throws SQLException {
		
		dcm = new DBConnectionManager();
		conn = dcm.getConnection();
		flag = false;
		
		try {
			sqlStr = "insert into org_i(org_i_name) values(?)";
			prepstmt = conn.prepareStatement(sqlStr);
			prepstmt.setString(1, orgization.getOrgIName());
			effectLines = prepstmt.executeUpdate();
			if (effectLines != 0) {
				flag = true;
			} 	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		  }
		
		if (flag) {
			try {
				sqlStr = "insert into org_ii(org_ii_name) values(?)";
				prepstmt = conn.prepareStatement(sqlStr);
				prepstmt.setString(1, orgization.getOrgIiName());
				effectLines = prepstmt.executeUpdate();
				if (effectLines != 0) {
					flag = true;
				} 	
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			  }
		}
		
		if (flag) {
			try {
				sqlStr = "insert into org_iii(org_iii_name) values(?)";
				prepstmt = conn.prepareStatement(sqlStr);
				prepstmt.setString(1, orgization.getOrgIiiName());
				effectLines = prepstmt.executeUpdate();
				if (effectLines != 0) {
					flag = true;
				} 	
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			  }
		}
		prepstmt.close();
		conn.close();
		return flag;
	}
	
	public boolean setPos(Position position) throws SQLException {
		
		int pos_type_id = 0;
		
		dcm = new DBConnectionManager();
		conn = dcm.getConnection();
		flag = false;
		
		try {
			sqlStr = "select id from pos_type where position_type_name=?";
			prepstmt = conn.prepareStatement(sqlStr);
			prepstmt.setString(1, position.getPositionTypeName());
			rs = prepstmt.executeQuery();
			if (rs.next()) {
				pos_type_id = rs.getInt(1);
				
			} 	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		  }
		
		if (pos_type_id != 0) {
			try {
				
				sqlStr = "insert into t_position(position_name, pos_type_id) values(?,?)";
				prepstmt = conn.prepareStatement(sqlStr);
				prepstmt.setString(1, position.getPositionName());
				prepstmt.setInt(2, pos_type_id);
				effectLines = prepstmt.executeUpdate();
				
				/*
				sqlStr = "insert into t_position(position_name, pos_type_id) values('"+position.getPosition_name()+"','"+pos_type_id+"')";
				stmt = conn.createStatement();
				effectLines = stmt.executeUpdate(sqlStr);
				*/
				if (effectLines != 0) {
					flag = true;
				} 	
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			  }
		}
		rs.close();
		prepstmt.close();
		conn.close();
		return flag;	
	}
	
}
